{% extends "base.html" %}

{% block title %}编辑招生{% endblock %}

{% block styles %}
<!-- 添加 Select2 的 CSS -->
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" rel="stylesheet" />
<style>
    .select2-container {
        width: 100% !important;
    }
    /* 修复 input-group 中的 Select2 样式 */
    .input-group > .select2-container--bootstrap-5 {
        flex: 1 1 auto;
        width: 1% !important;
    }
    .input-group > .select2-container--bootstrap-5 .select2-selection {
        height: 100%;
        line-height: inherit;
        border-radius: 0.375rem 0 0 0.375rem;
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-8 offset-md-2">
        <h2 class="mb-4">编辑招生</h2>
        <form method="POST">
            <div class="mb-3">
                <label for="student_id" class="form-label">选择学生</label>
                <div class="input-group">
                    <select class="form-control" id="student_id" name="student_id" required>
                        <option value="">请选择学生</option>
                        {% for student in students %}
                        <option value="{{ student.id }}" {% if student.id == registration.student_id %}selected{% endif %}>
                            {{ student.name }} ({{ student.parent_name }}的{{ student.parent_relation }})
                        </option>
                        {% endfor %}
                    </select>
                    <a href="{{ url_for('add_student') }}" class="btn btn-primary" target="_blank">添加新学生</a>
                    <button type="button" class="btn btn-secondary" onclick="location.reload()">
                        <i class="fas fa-sync-alt"></i> 刷新
                    </button>
                </div>
            </div>
            <div class="mb-3">
                <label for="class_id" class="form-label">选择班级</label>
                <div class="input-group">
                    <select class="form-control" id="class_id" name="class_id" required>
                        <option value="">请选择班级</option>
                        {% for class_obj in classes %}
                        <option value="{{ class_obj.id }}" {% if class_obj.id == registration.class_id %}selected{% endif %}>
                            {{ class_obj.grade }} {{ class_obj.year }} {{ class_obj.semester }}
                        </option>
                        {% endfor %}
                    </select>
                    <a href="{{ url_for('add_class') }}" class="btn btn-primary" target="_blank">添加新班级</a>
                    <button type="button" class="btn btn-secondary" onclick="location.reload()">
                        <i class="fas fa-sync-alt"></i> 刷新
                    </button>
                </div>
            </div>
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">保存</button>
                <a href="{{ url_for('list_registrations') }}" class="btn btn-secondary">返回</a>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<!-- 添加 Select2 的 JS -->
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/i18n/zh-CN.js"></script>
<script>
$(document).ready(function() {
    // 初始化学生选择框
    $('#student_id').select2({
        theme: 'bootstrap-5',
        language: 'zh-CN',
        placeholder: '请选择学生',
        allowClear: true,
        dropdownParent: $('#student_id').parent()
    });
});

// 监听新窗口关闭事件，刷新下拉列表
window.onmessage = function(e) {
    if (e.data === 'refresh') {
        location.reload();
    }
};
</script>
{% endblock %} 